/*
//  JTRule.m
//  Agate Rule Tester
//
//  Created by Jim Turner on 12/9/05.
//

License: GNU General Public License
This program is free software; you can redistribute it and/or modify it under 
the terms of the GNU General Public License as published by the Free Software 
Foundation; either version 2 of the License, or (at your option) any later 
version.

This program is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
details.

You should have received a copy of the GNU General Public License along 
with this program; if not, write to the Free Software Foundation, Inc., 
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
http://www.gnu.org/licenses/licenses.html#GPL

*/

#import "JTRule.h"


@implementation JTRule

#pragma mark rule initialization/deallocation
-(id) init
{
	if( self = [super init] )
	{
		// Nothin' doin' right now
		NSLog( @"I'm alive!!" );
	}
	
	return( self );
}

-(void) dealloc
{
	NSLog( @"I'm melting... melting!!!" );
	[find release];
	[replace release];
	[translationInfo release];
	
	[super dealloc];
}

// Convenience method that effectively duplicates the calling object
- (id)copyWithZone:(NSZone *)zone
{
	JTRule *copyOfRule = [[[self class] allocWithZone:zone] initWithRule:self];

	return( copyOfRule );
}

- (id)initWithRule:(JTRule *)rule
{
	if( self = [super init] )
	{
		// 'self' here is the newly-formed copy of the rule.
		// 'rule' is the rule being copied (see copyWithZone: above)
		[self setActive:[rule activeState]];
		[self setFind:[rule find]];
		[self setReplace:[rule replace]];
		[self setIsCaseSensitive:[rule isCaseSensitive]];
		[self setIsExhaustiveReplace:[rule isExhaustiveReplace]];
		[self setIsReplaceFirstOnly:[rule isReplaceFirstOnly]];
		[self setIsReplaceLastOnly:[rule isReplaceLastOnly]];
		[self setIsRegex:[rule isRegex]];
		[self setTranslationInformation:[rule translationInformation]];
	}
	
	return( self );
}

- (id)initWithDictionary:(NSDictionary *)ruleDict
{
	if( self = [super init] )
	{
		[self setActive:[[ruleDict objectForKey:@"active"] intValue]];
		[self setFind:[ruleDict objectForKey:@"lookfor"]];
		[self setReplace:[ruleDict objectForKey:@"changeto"]];
		[self setIsCaseSensitive:[[ruleDict objectForKey:@"casesensitive"] boolValue]];
		[self setIsExhaustiveReplace:[[ruleDict objectForKey:@"exhaustivereplace"] boolValue]];
		[self setIsReplaceFirstOnly:[[ruleDict objectForKey:@"replacefirst"] boolValue]];
		[self setIsReplaceLastOnly:[[ruleDict objectForKey:@"replacelast"] boolValue]];
		[self setIsRegex:[[ruleDict objectForKey:@"isregex"] boolValue]];
		[self setTranslationInformation:[ruleDict objectForKey:@"translationinfo"]];
	}
	
	return( self );
}

#pragma mark Setter functions
-(void) setActive:(int) state
{
	activeState = state;
}

-(void) setFind:(NSString *) string
{
	[find autorelease];
	
	find = [string copy];
}

-(void) setReplace:(NSString *) string
{
	[replace autorelease];

	replace = [string copy];
}

-(void) setIsCaseSensitive:(BOOL) state
{
	caseSensitive = state;
}

-(void) setIsExhaustiveReplace:(BOOL) state
{
	exhaustiveReplace = state;
}

-(void) setIsReplaceFirstOnly:(BOOL) state
{
	replaceFirst = state;
}

-(void) setIsReplaceLastOnly:(BOOL) state
{
	replaceLast = state;
}

-(void) setIsRegex:(BOOL) state
{
	perlRegex = state;
}

-(void) setTranslationInformation:(NSString *) string
{
	[translationInfo autorelease];
	
	translationInfo = [string copy];
}

#pragma mark Getter functions
-(int) activeState
{
	return( activeState );
}

-(NSString *) find
{
	return( find );
}

-(NSString *) replace
{
	return( replace );
}

-(BOOL) isCaseSensitive
{
	return( caseSensitive );
}

-(BOOL) isExhaustiveReplace
{
	return( exhaustiveReplace );
}

-(BOOL) isReplaceFirstOnly
{
	return( replaceFirst );
}

-(BOOL) isReplaceLastOnly
{
	return( replaceLast );
}

-(BOOL) isRegex
{
	return( perlRegex );
}

-(NSString *) translationInformation
{
	return( translationInfo );
}

#pragma mark Interface functions
-(NSDictionary *) returnObjectAsDictionary
{
	NSDictionary *tmp = [NSDictionary dictionaryWithObjectsAndKeys:
								[NSNumber numberWithInt:[self activeState]], @"active",
								[self find], @"lookfor",
								[self replace], @"changeto",
								[NSNumber numberWithBool:[self isCaseSensitive]], @"casesensitive",
								[NSNumber numberWithBool:[self isExhaustiveReplace]], @"exhaustivereplace",
								[NSNumber numberWithBool:[self isReplaceFirstOnly]], @"replacefirst",
								[NSNumber numberWithBool:[self isReplaceLastOnly]], @"replacelast",
								[NSNumber numberWithBool:[self isRegex]], @"isregex",
								[self translationInformation], @"translationinfo",
								nil];
	
	return( tmp );
}

#pragma mark Object Description
-(NSString *)description
{
	return( [[self returnObjectAsDictionary] description] );
}

@end
